/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.aop.handler.java;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Objects;
import java.util.logging.Logger;

import com.hyts.aop.annotation.Before;
import com.hyts.aop.annotation.Pointcut;
import com.hyts.aop.base.BaseAopHandler;
import com.hyts.aop.bean.ProcessArg;
import com.hyts.aop.error.AopException;
import com.hyts.aop.handler.base.AfterMethod;
import com.hyts.aop.handler.base.BeforeMethod;
import com.hyts.aop.handler.base.RoundMethod;
import com.hyts.aop.handler.base.ValidateMethod;
import com.hyts.aop.handler.reflection.*;

/** 
 * @title JdkAopHandler.java  
 * @package com.hyts.aop.handler  
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com  
 * @date 2018年4月24日 
 * @version V1.0 
 */
public class JdkAopHandler extends BaseAopHandler implements InvocationHandler {
	

	/**
	 * @constructor：BeforeAopHandler
	 * @param proxyRealObject
	 */
	public JdkAopHandler(Object... proxyRealObject) {
		super(proxyRealObject);
		aopWeaveObject = proxyRealObject;
		ReflectionFacadeHandler.setBeforeMethodHandler(new BeforeMethodHandler(aopWeaveObject));
		ReflectionFacadeHandler.setAfterMethodHandler(new AfterMethodHandler(aopWeaveObject));
		ReflectionFacadeHandler.setRoundMethodHandler(new RoundMethodHandler(aopWeaveObject));
		ReflectionFacadeHandler.setPointcutMethodHandler(new ValidateMethodHandler(proxyRealObject));
	}

	/**
	 * @constructor：DefaultAopHandler
	 * @param proxyRealObject
	 * @param aopWeaveObject
	 */
	public JdkAopHandler(Object proxyRealObject,Object... aopWeaveObject) {
		super(proxyRealObject);
		ReflectionFacadeHandler.setBeforeMethodHandler(new BeforeMethodHandler(aopWeaveObject));
		ReflectionFacadeHandler.setAfterMethodHandler(new AfterMethodHandler(aopWeaveObject));
		ReflectionFacadeHandler.setRoundMethodHandler(new RoundMethodHandler(aopWeaveObject));
		ReflectionFacadeHandler.setPointcutMethodHandler(new ValidateMethodHandler(proxyRealObject));
	}
	/*
	 * @param proxy
	 * @param method
	 * @param args
	 * @return
	 * @throws Throwable
	 * @see com.hyts.aop.handler.BaseAopHandler#invoke(java.lang.Object,
	 * java.lang.reflect.Method, java.lang.Object[])
	 * @exception
	 */
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		//校验是否被选中为忽略状态
		Object result = null;
		ProcessArg processArg = null;
		if(ReflectionFacadeHandler.getPointcutMethodHandler().validate(method) ) {
			ReflectionFacadeHandler.getBeforeMethodHandler().before(args);
			processArg = ReflectionFacadeHandler.getRoundMethodHandler().round(method,this.getProxyRealObject(),args);
		}
		/*if(Objects.nonNull(processArg) && !processArg.isExecuted()){

		}*/
		result = method.invoke(this.getProxyRealObject(), args);
		if(ReflectionFacadeHandler.getPointcutMethodHandler().validate(method)) {
			ReflectionFacadeHandler.getRoundMethodHandler().round(method, this.getProxyRealObject(), args);
		}
		if(ReflectionFacadeHandler.getPointcutMethodHandler().validate(method)) {
			ReflectionFacadeHandler.getAfterMethodHandler().after(args);
		}
		return result;
	}


	/**  
     * @param clazz
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @exception
     */ 
    @SuppressWarnings("unchecked")
    public <T> T build(T clazz) throws AopException{
        return (T)Proxy.newProxyInstance(getProxyRealObject().getClass().getClassLoader(),getProxyRealObject().getClass().getInterfaces(),this);
    }
    
    /**  
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @exception
     */ 
    public Object build() throws AopException {
        return Proxy.newProxyInstance(getProxyRealObject().getClass().getClassLoader(),getProxyRealObject().getClass().getInterfaces(),this);
    }
	
}
